package com.example.other.netty.pipehandtest;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class TestServer {
    public static void main(String[] args) {
        ServerBootstrap serverBootstrap = new ServerBootstrap();

        NioEventLoopGroup ggroup = new NioEventLoopGroup();
        NioEventLoopGroup wgroup = new NioEventLoopGroup();
        serverBootstrap.group(ggroup, wgroup)
                .channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 1024)
                .childOption(ChannelOption.SO_KEEPALIVE, true)
                .childOption(ChannelOption.TCP_NODELAY, true)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
                        //inBound 处理读数据的逻辑链
                        nioSocketChannel.pipeline().addLast(new AInServerHandler());
                        nioSocketChannel.pipeline().addLast(new BInServerHandler());
                        nioSocketChannel.pipeline().addLast(new CInServerHandler());
                        /**
                         * 执行结果：
                         * A:PooledUnsafeDirectByteBuf(ridx: 0, widx: 122, cap: 1024)
                         * B:PooledUnsafeDirectByteBuf(ridx: 0, widx: 122, cap: 1024)
                         * C:PooledUnsafeDirectByteBuf(ridx: 0, widx: 122, cap: 1024)
                         * 所以执行结果与addLast的添加顺序一致
                         */

                        //outBound 处理写数据的逻辑链
                        nioSocketChannel.pipeline().addLast(new AOutServerHandler());
                        nioSocketChannel.pipeline().addLast(new BOutServerHandler());
                        nioSocketChannel.pipeline().addLast(new COutServerHandler());

                    }
                });
        serverBootstrap.bind(8088).addListener(future->{
            if(future.isSuccess()){
                System.out.println("success ON");
            }else{
                System.out.println("fail ON");
            }
        });
    }
}
